# -*- coding: utf-8 -*-
from django.http import HttpResponse
from django.shortcuts import render
from django.conf import settings

from django.http import JsonResponse, HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse

from .models import Images


def home(request):
    return render(request, "home_application/index_home.html")

from django.http import JsonResponse, HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse

from .models import Images


def home(request):
    """
    首页
    """
    return render(request, "home_application/index_home.html")


def upload(request):
    """
    上传图片
    """
    # 获取图片
    uploded_file = request.FILES.get("pics")
    if uploded_file:
        # 如果图片存在则将该图片信息通过ImageField存入数据库
        Images.objects.create(image=uploded_file)
    # 当图片存储之后，重定向到首页
    return HttpResponseRedirect(reverse('doc:home'))


def download(request, image_id):
    """
    下载图片
    """
    image = Images.objects.filter(id=image_id).first()
    # 获取图片的url地址，传给前端
    url = image.image.url
    return JsonResponse({
        "result": True,
        "code": 200,
        "data": {
            "url": url
        }
    })


def fileup(request):
    """
    首页
    """
    # 获取数据库中前20个图片
    images = Images.objects.all()[:20]
    image_list = []
    # 循环获取所有的图片
    for image in images:
        # 将数据进行格式化后传到前端
        image_list.append({
            "image_url": image.image.url,  # 通过image.image.url获取图片的路径
            "image_id": image.id
        })
    return render(request, "home_application/fileup.html", {
        "image_list": image_list
    })


def upload(request):
    """
    上传图片
    """
    # 获取图片
    uploded_file = request.FILES.get("pics")
    if uploded_file:
        # 如果图片存在则将该图片信息通过ImageField存入数据库
        Images.objects.create(image=uploded_file)
    # 当图片存储之后，重定向到首页
    return HttpResponseRedirect(reverse('doc:home'))


def download(request, image_id):
    """
    下载图片
    """
    image = Images.objects.filter(id=image_id).first()
    # 获取图片的url地址，传给前端
    url = image.image.url
    return JsonResponse({
        "result": True,
        "code": 200,
        "data": {
            "url": url
        }
    })


def dev_guide(request):
    return render(request, "home_application/dev_guide.html")


def contact(request):
    return render(request, "home_application/contact.html")


def index(request):
    return render(request, "homepage.html")


def homework1(request):
    return render(request, "python/homework1.html")


def homework2(request):
    return render(request, "python/homework2.html")


def homework3(request):
    return render(request, "python/homework3.html")


def frontend(request):
    return render(request, "frontend.html")


def adhomework1(request):
    return render(request, "advanced/homework1.html")


def adhomework2(request):
    return render(request, "advanced/homework2.html")


def adhomework3(request):
    return render(request, "advanced/homework3.html")
